<template>
  <div class="temperature-converter">
    <div class="converter-header">
      <h2>🌡️ 智能温度转换器</h2>
      <p class="subtitle">响应式数据绑定 + 计算属性实战</p>
    </div>
    
    <div class="converter-body">
      <div class="input-section">
        <div class="temperature-input-group">
          <div class="input-card celsius-card">
            <div class="input-header">
              <span class="temperature-icon">🌡️</span>
              <h3>摄氏度 (°C)</h3>
            </div>
            <div class="input-control">
              <input
                v-model.number="celsius"
                type="number"
                placeholder="输入摄氏度"
                class="temperature-input"
                @input="handleCelsiusInput"
              >
              <span class="unit">°C</span>
            </div>
            <div class="temperature-status">
              <div class="status-indicator" :class="celsiusStatus.class">
                {{ celsiusStatus.text }}
              </div>
            </div>
          </div>
          
          <div class="conversion-arrow">
            <div class="arrow-icon">⇄</div>
            <div class="conversion-formula">
              {{ conversionFormula }}
            </div>
          </div>
          
          <div class="input-card fahrenheit-card">
            <div class="input-header">
              <span class="temperature-icon">🌡️</span>
              <h3>华氏度 (°F)</h3>
            </div>
            <div class="input-control">
              <input
                v-model.number="fahrenheit"
                type="number"
                placeholder="输入华氏度"
                class="temperature-input"
                @input="handleFahrenheitInput"
              >
              <span class="unit">°F</span>
            </div>
            <div class="temperature-status">
              <div class="status-indicator" :class="fahrenheitStatus.class">
                {{ fahrenheitStatus.text }}
              </div>
            </div>
          </div>
        </div>
      </div>
      
      <div class="result-section">
        <div class="result-card">
          <h4>🎯 转换结果</h4>
          <div class="result-grid">
            <div class="result-item">
              <span class="result-label">摄氏度:</span>
              <span class="result-value">{{ celsius.toFixed(1) }}°C</span>
            </div>
            <div class="result-item">
              <span class="result-label">华氏度:</span>
              <span class="result-value">{{ fahrenheit.toFixed(1) }}°F</span>
            </div>
            <div class="result-item">
              <span class="result-label">开尔文:</span>
              <span class="result-value">{{ kelvin.toFixed(1) }}K</span>
            </div>
          </div>
        </div>
        
        <div class="temperature-scale">
          <h4>🌡️ 温度对照表</h4>
          <div class="scale-items">
            <div 
              v-for="item in temperatureScale" 
              :key="item.celsius"
              class="scale-item"
              :class="{ active: isTemperatureNear(item.celsius) }"
            >
              <span class="scale-celsius">{{ item.celsius }}°C</span>
              <span class="scale-fahrenheit">{{ item.fahrenheit }}°F</span>
              <span class="scale-description">{{ item.description }}</span>
            </div>
          </div>
        </div>
      </div>
      
      <div class="history-section">
        <div class="history-header">
          <h4>📊 转换历史</h4>
          <button @click="clearHistory" class="clear-btn">🗑️ 清空</button>
        </div>
        <div class="history-list">
          <div 
            v-for="(record, index) in conversionHistory" 
            :key="index"
            class="history-item"
          >
            <div class="history-time">{{ record.time }}</div>
            <div class="history-values">
              <span>{{ record.celsius }}°C</span>
              <span class="arrow">→</span>
              <span>{{ record.fahrenheit }}°F</span>
            </div>
          </div>
          <div v-if="conversionHistory.length === 0" class="empty-history">
            暂无转换记录
          </div>
        </div>
      </div>
      
      <div class="controls-section">
        <div class="control-buttons">
          <button @click="setCommonTemperature('freezing')" class="control-btn">
            🧊 冰点 (0°C)
          </button>
          <button @click="setCommonTemperature('body')" class="control-btn">
            🏃 体温 (37°C)
          </button>
          <button @click="setCommonTemperature('boiling')" class="control-btn">
            ♨️ 沸点 (100°C)
          </button>
          <button @click="swapTemperatures" class="control-btn">
            🔄 交换数值
          </button>
        </div>
      </div>
    </div>
    
    <div class="converter-footer">
      <div class="features">
        <div class="feature-item">
          <span class="feature-icon">⚡</span>
          <span>响应式数据绑定</span>
        </div>
        <div class="feature-item">
          <span class="feature-icon">🧮</span>
          <span>自动计算转换</span>
        </div>
        <div class="feature-item">
          <span class="feature-icon">📊</span>
          <span>历史记录追踪</span>
        </div>
        <div class="feature-item">
          <span class="feature-icon">🎯</span>
          <span>实时状态反馈</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, watch } from 'vue'

// 响应式温度数据
const celsius = ref<number>(25)
const fahrenheit = ref<number>(77)
const lastInput = ref<'celsius' | 'fahrenheit' | null>(null)
const conversionHistory = ref<Array<{
  celsius: number
  fahrenheit: number
  time: string
}>>([])

// 温度转换公式
const conversionFormula = computed(() => {
  return 'F = C × 9/5 + 32'
})

// 开尔文温度计算
const kelvin = computed(() => {
  return celsius.value + 273.15
})

// 摄氏度状态
const celsiusStatus = computed(() => {
  const temp = celsius.value
  if (temp <= 0) return { class: 'cold', text: '❄️ 冰点以下' }
  if (temp <= 20) return { class: 'cool', text: '🌤️ 凉爽' }
  if (temp <= 30) return { class: 'warm', text: '☀️ 温暖' }
  if (temp <= 40) return { class: 'hot', text: '🔥 炎热' }
  return { class: 'very-hot', text: '🌋 酷热' }
})

// 华氏度状态
const fahrenheitStatus = computed(() => {
  const temp = fahrenheit.value
  if (temp <= 32) return { class: 'cold', text: '❄️ 冰点以下' }
  if (temp <= 68) return { class: 'cool', text: '🌤️ 凉爽' }
  if (temp <= 86) return { class: 'warm', text: '☀️ 温暖' }
  if (temp <= 104) return { class: 'hot', text: '🔥 炎热' }
  return { class: 'very-hot', text: '🌋 酷热' }
})

// 温度对照表
const temperatureScale = computed(() => [
  { celsius: -20, fahrenheit: -4, description: '极寒' },
  { celsius: -10, fahrenheit: 14, description: '严寒' },
  { celsius: 0, fahrenheit: 32, description: '冰点' },
  { celsius: 10, fahrenheit: 50, description: '寒冷' },
  { celsius: 20, fahrenheit: 68, description: '凉爽' },
  { celsius: 25, fahrenheit: 77, description: '舒适' },
  { celsius: 30, fahrenheit: 86, description: '温暖' },
  { celsius: 37, fahrenheit: 98.6, description: '体温' },
  { celsius: 40, fahrenheit: 104, description: '炎热' },
  { celsius: 50, fahrenheit: 122, description: '酷热' },
  { celsius: 60, fahrenheit: 140, description: '极热' },
  { celsius: 80, fahrenheit: 176, description: '沙漠' },
  { celsius: 100, fahrenheit: 212, description: '沸点' }
])

// 检查温度是否接近对照表中的某个值
const isTemperatureNear = (refTemp: number): boolean => {
  return Math.abs(celsius.value - refTemp) <= 2
}

// 摄氏度输入处理
const handleCelsiusInput = () => {
  lastInput.value = 'celsius'
  fahrenheit.value = Number((celsius.value * 9/5 + 32).toFixed(1))
  addToHistory()
}

// 华氏度输入处理
const handleFahrenheitInput = () => {
  lastInput.value = 'fahrenheit'
  celsius.value = Number(((fahrenheit.value - 32) * 5/9).toFixed(1))
  addToHistory()
}

// 添加到历史记录
const addToHistory = () => {
  const now = new Date()
  const timeString = now.toLocaleTimeString('zh-CN', { 
    hour: '2-digit', 
    minute: '2-digit', 
    second: '2-digit' 
  })
  
  conversionHistory.value.unshift({
    celsius: Number(celsius.value.toFixed(1)),
    fahrenheit: Number(fahrenheit.value.toFixed(1)),
    time: timeString
  })
  
  // 限制历史记录数量
  if (conversionHistory.value.length > 10) {
    conversionHistory.value = conversionHistory.value.slice(0, 10)
  }
}

// 清空历史记录
const clearHistory = () => {
  conversionHistory.value = []
}

// 设置常用温度
const setCommonTemperature = (type: 'freezing' | 'body' | 'boiling') => {
  const temps = {
    freezing: { celsius: 0, fahrenheit: 32 },
    body: { celsius: 37, fahrenheit: 98.6 },
    boiling: { celsius: 100, fahrenheit: 212 }
  }
  
  celsius.value = temps[type].celsius
  fahrenheit.value = temps[type].fahrenheit
  addToHistory()
}

// 交换温度数值
const swapTemperatures = () => {
  const temp = celsius.value
  celsius.value = Number(((fahrenheit.value - 32) * 5/9).toFixed(1))
  fahrenheit.value = Number((temp * 9/5 + 32).toFixed(1))
  addToHistory()
}

// 监听温度变化（用于调试和扩展功能）
watch([celsius, fahrenheit], ([newCelsius, newFahrenheit]) => {
  console.log(`🌡️ 温度更新: ${newCelsius}°C = ${newFahrenheit}°F`)
})

// 初始化时添加到历史记录
addToHistory()
</script>

<style scoped>
.temperature-converter {
  max-width: 900px;
  margin: 30px auto;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 20px;
  padding: 30px;
  color: white;
  font-family: 'Arial', sans-serif;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
}

.converter-header {
  text-align: center;
  margin-bottom: 30px;
}

.converter-header h2 {
  font-size: 28px;
  margin-bottom: 10px;
  background: linear-gradient(45deg, #fff, #42b883);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.subtitle {
  opacity: 0.8;
  font-size: 16px;
}

.converter-body {
  display: flex;
  flex-direction: column;
  gap: 25px;
}

.input-section {
  background: rgba(255, 255, 255, 0.1);
  padding: 25px;
  border-radius: 15px;
}

.temperature-input-group {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
}

.input-card {
  flex: 1;
  background: rgba(255, 255, 255, 0.05);
  padding: 20px;
  border-radius: 12px;
  text-align: center;
}

.input-header {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-bottom: 15px;
}

.temperature-icon {
  font-size: 24px;
}

.input-header h3 {
  font-size: 18px;
  color: #42b883;
}

.input-control {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 15px;
}

.temperature-input {
  flex: 1;
  padding: 12px;
  border: none;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.9);
  color: #333;
  font-size: 16px;
  text-align: center;
}

.unit {
  font-size: 18px;
  font-weight: bold;
  color: #42b883;
}

.temperature-status {
  margin-top: 10px;
}

.status-indicator {
  padding: 8px 16px;
  border-radius: 20px;
  font-size: 14px;
  font-weight: bold;
  text-align: center;
}

.status-indicator.cold {
  background: rgba(0, 123, 255, 0.3);
  color: #007bff;
}

.status-indicator.cool {
  background: rgba(40, 167, 69, 0.3);
  color: #28a745;
}

.status-indicator.warm {
  background: rgba(255, 193, 7, 0.3);
  color: #ffc107;
}

.status-indicator.hot {
  background: rgba(253, 126, 20, 0.3);
  color: #fd7e14;
}

.status-indicator.very-hot {
  background: rgba(220, 53, 69, 0.3);
  color: #dc3545;
}

.conversion-arrow {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.arrow-icon {
  font-size: 32px;
  color: #42b883;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

.conversion-formula {
  font-size: 12px;
  opacity: 0.8;
  text-align: center;
}

.result-section {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
}

.result-card, .temperature-scale {
  background: rgba(255, 255, 255, 0.1);
  padding: 20px;
  border-radius: 12px;
}

.result-card h4, .temperature-scale h4 {
  margin-bottom: 15px;
  color: #42b883;
  font-size: 16px;
}

.result-grid {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.result-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px;
  background: rgba(255, 255, 255, 0.05);
  border-radius: 8px;
}

.result-label {
  font-weight: bold;
}

.result-value {
  font-size: 18px;
  color: #42b883;
  font-weight: bold;
}

.scale-items {
  display: flex;
  flex-direction: column;
  gap: 5px;
  max-height: 200px;
  overflow-y: auto;
}

.scale-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 12px;
  background: rgba(255, 255, 255, 0.05);
  border-radius: 6px;
  font-size: 12px;
  transition: all 0.3s ease;
}

.scale-item.active {
  background: rgba(66, 184, 131, 0.3);
  border: 1px solid #42b883;
}

.scale-celsius {
  font-weight: bold;
  color: #42b883;
}

.scale-fahrenheit {
  opacity: 0.8;
}

.scale-description {
  font-size: 11px;
  opacity: 0.7;
}

.history-section {
  background: rgba(255, 255, 255, 0.1);
  padding: 20px;
  border-radius: 12px;
}

.history-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.history-header h4 {
  color: #42b883;
  font-size: 16px;
}

.clear-btn {
  padding: 8px 12px;
  border: none;
  border-radius: 6px;
  background: rgba(220, 53, 69, 0.3);
  color: #dc3545;
  cursor: pointer;
  font-size: 12px;
  transition: all 0.3s ease;
}

.clear-btn:hover {
  background: rgba(220, 53, 69, 0.5);
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 150px;
  overflow-y: auto;
}

.history-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px;
  background: rgba(255, 255, 255, 0.05);
  border-radius: 6px;
  font-size: 12px;
}

.history-time {
  opacity: 0.7;
}

.history-values {
  display: flex;
  align-items: center;
  gap: 8px;
}

.history-values .arrow {
  color: #42b883;
  font-weight: bold;
}

.empty-history {
  text-align: center;
  opacity: 0.6;
  padding: 20px;
}

.controls-section {
  background: rgba(255, 255, 255, 0.1);
  padding: 20px;
  border-radius: 12px;
}

.control-buttons {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 10px;
}

.control-btn {
  padding: 12px;
  border: none;
  border-radius: 8px;
  background: rgba(66, 184, 131, 0.3);
  color: #42b883;
  cursor: pointer;
  font-size: 12px;
  transition: all 0.3s ease;
}

.control-btn:hover {
  background: rgba(66, 184, 131, 0.5);
  transform: translateY(-2px);
}

.converter-footer {
  margin-top: 30px;
  padding-top: 20px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.features {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 15px;
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: center;
  padding: 10px;
  background: rgba(255, 255, 255, 0.05);
  border-radius: 8px;
  font-size: 12px;
}

.feature-icon {
  font-size: 16px;
}

@media (max-width: 768px) {
  .temperature-converter {
    padding: 20px;
    margin: 20px;
  }
  
  .temperature-input-group {
    flex-direction: column;
    gap: 15px;
  }
  
  .conversion-arrow {
    order: 2;
    flex-direction: row;
    justify-content: center;
  }
  
  .result-section {
    grid-template-columns: 1fr;
  }
  
  .control-buttons {
    grid-template-columns: repeat(2, 1fr);
  }
}
</style>